import { ref } from 'vue'
import { defineStore } from 'pinia'
import { userProFileService } from '@/api/user.js'

export const useUserStore = defineStore('useUserStore', ()=>{
  const token = ref('')
  const userInfo = ref([])
  const remember = ref(false)
  const rememberInfo = ref({
    username: '',
    password: '',
  })

  const setToken = (s) => {
    token.value = s
  }
  const resetToken = () => {
    token.value = ''
  }

  const renewInfo = async () => {
    const info = await userProFileService()
    userInfo.value = info.data.data
    console.log('info', userInfo.value)
  }

  const resetInfo = () =>{
    userInfo.value = []
  }

  const storeRememberInfo = (username, password) => {
    if (remember.value) {
      rememberInfo.value.username = username
      rememberInfo.value.password = password
    }
    else {
      resetRememberInfo()
    }

  }

  const resetRememberInfo = () =>{
    rememberInfo.value.username = ''
    rememberInfo.value.password = ''
  }

  return {token, setToken, resetToken, userInfo, renewInfo, resetInfo, remember, rememberInfo, storeRememberInfo, resetRememberInfo}
},{
  persist: true,
})
